package com.kingkim.electromechanical.usercenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kingkim.electromechanical.commonutils.model.PageBean;
import com.kingkim.electromechanical.commonutils.util.Common;
import com.kingkim.electromechanical.usercenter.mapper.RoleMapper;
import com.kingkim.electromechanical.usercenter.model.po.Role;
import com.kingkim.electromechanical.usercenter.service.IRoleService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Resource
    RoleMapper roleMapper;


    @Override
    public PageBean QueryForList(int currentPage,int pageSize,Role role)  throws DataAccessException {
        PageHelper.startPage(currentPage,pageSize);
        List roleList = roleMapper.QueryForList(role);
        PageInfo pi = new PageInfo<Role>(roleList);
        PageBean page = new PageBean<Role>();
        page.setCurrentPage(pi.getPageNum());
        page.setPageCount(pi.getPages());
        page.setList(pi.getList());
        page.setTotalCount(pi.getTotal());
        return page;
    }

    @Override
    public int DeleteForList(List<String> ids) throws DataAccessException {
        return roleMapper.DeleteForList(ids);
    }

    @Override
    public int SaveOrModify(Role role) throws DataAccessException {
        if(StringUtils.isNotBlank(role.getAccount_role_id())){
            return roleMapper.Modify(role);
        }else{
            role.setAccount_role_id(Common.GetUUID());
            role.setCreate_time(Common.DateToString("yyyy-MM-dd"));
            return roleMapper.Save(role);
        }

    }
}
